package us.gaaoc.framework.util.zip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * Utility for zipping and unzipping files for Fulton County.
 * 
 * @author diazr
 * 
 */

public class ZipUtil {
	/**
	 * Function that compresses a list of files to a zip file.
	 * 
	 * @param fileName
	 * @param targetFiles
	 */
	public static void compressTargetFiles(String fileName,
			String[] targetFiles) {
		int BUFFER = 2048;
		byte data[] = new byte[BUFFER];
		File file = new File(fileName);

		try {
			file.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}

		FileOutputStream fo = null;
		try {
			fo = new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		ZipOutputStream zipOut = new ZipOutputStream(fo);

		for (String s : targetFiles) {
			try {
				FileInputStream in = new FileInputStream(s);
				System.out.println("adding: " + s);

				String[] l = s.split("/");

				String temp = l[l.length - 1];
				ZipEntry zip = new ZipEntry(temp);
				zipOut.putNextEntry(zip);

				int count;
				while ((count = in.read(data)) != -1) {
					zipOut.write(data, 0, count);
				}

				in.close();
			} catch (FileNotFoundException ex) {
				ex.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			zipOut.close();
			fo.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * Function that decompresses a zip file to a target directory.
	 * 
	 * @param targetZip
	 * @param targetDirectory
	 */
	public static void decompressTargetZipToDirectory(String targetZip,
			String targetDirectory) {
		final int BUFFER = 2048;
		try {
			BufferedOutputStream dest = null;
			BufferedInputStream is = null;
			ZipEntry entry;
			ZipFile zipfile = new ZipFile(targetZip);
			Enumeration e = zipfile.entries();
			
			while (e.hasMoreElements()) {
				entry = (ZipEntry) e.nextElement();
				System.out.println("Extracting: " + entry);
				is = new BufferedInputStream(zipfile.getInputStream(entry));
				int count;
				byte data[] = new byte[BUFFER];
				FileOutputStream fos = new FileOutputStream(targetDirectory + entry.getName());
				dest = new BufferedOutputStream(fos, BUFFER);
				while ((count = is.read(data, 0, BUFFER)) != -1) {
					dest.write(data, 0, count);
				}
				dest.flush();
				dest.close();
				is.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String args[]) {

		String[] l = new String[19];
		l[0] = "/home/diazr/gajelib/lib/annogen-0.1.0.jar";
		l[1] = "/home/diazr/gajelib/lib/chartcreator-1.2.0.jar";
		l[2] = "/home/diazr/gajelib/lib/commons-logging-1.0.4.jar";
		l[3] = "/home/diazr/gajelib/lib/jcommon-1.0.12.jar";
		l[4] = "/home/diazr/gajelib/lib/ldap.jar";
		l[5] = "/home/diazr/gajelib/lib/pom.xml";
		l[6] = "/home/diazr/gajelib/lib/antlr-2.7.6.jar";
		l[7] = "/home/diazr/gajelib/lib/CMS_Sustain_2.0_Bibb.jar";
		l[8] = "/home/diazr/gajelib/lib/CourtCalendarSimple-SchemaAbstraction.jar";
		l[9] = "/home/diazr/gajelib/lib/jfreechart-1.0.9-experimental.jar";
		l[10] = "/home/diazr/gajelib/lib/libstoget.txt";
		l[11] = "/home/diazr/gajelib/lib/pvjdbc2.jar";
		l[12] = "/home/diazr/gajelib/lib/antlr-2.7.6rc1.jar";
		l[13] = "/home/diazr/gajelib/lib/commons-beanutils-1.7.0.jar ";
		l[14] = "/home/diazr/gajelib/lib/jfreechart-1.0.9.jar";
		l[15] = "/home/diazr/gajelib/lib/log4j-1.2.13.jar";
		l[16] = "/home/diazr/gajelib/lib/pvjdbc2x.jar";
		l[17] = "/home/diazr/gajelib/lib/asm-attrs.jar";
		l[18] = "/home/diazr/gajelib/lib/commons-collections-3.1.jar";

		ZipUtil.compressTargetFiles("/home/diazr/asdf.zip", l);
		ZipUtil.decompressTargetZipToDirectory("/home/diazr/asdf.zip",
				"/home/diazr/temp/");
	}
}
